var n_before=localStorage.getItem('total_of_people');
var gap=window.innerHeight;
var operation_now='game';
if(n_before!=null){
	n=m1=m2=n_before;
	for(var i=0;i<n;i++){
		person[i]=localStorage.getItem('person'+String(i));
		vis[i]=localStorage.getItem('vis'+String(i));
		if(vis[i]==null) vis[i]=0;
	}
	$('.tip').html('当前共有'+String(localStorage.getItem('rest'))+'人未中奖');
}
$('.region1').css('height',String(gap)+'px');
$('.region2').css('height',String(gap)+'px');
$('.region3').css('height',String(gap)+'px');
$('.help').css('height',String(gap)+'px');

function get_random(){
	var id=Math.floor(Math.random()*n);
	while(vis[id]) id=Math.floor(Math.random()*n);
	return id;
}
function action(){
	var s='';
	var tmp=document.querySelector('#code0');
	var num=tmp.value;
	if(num=='clear'){
		localStorage.clear();
		location.reload();
	}
	else if(num=='console'){
		window.location.href="./console/console.html";
	}
	else if(num!=''){
		var n=localStorage.getItem('total_of_people');
		for(var i=1;i<=num;i++){
			var id=Math.floor(Math.random()*n);
			while(vis1[id]) id=Math.floor(Math.random()*n);
			s=s+person[id]+'<br>';
			vis1[id]=1;
		}
		for(var i=0;i<n;i++) vis1[i]=0;
		$('#first').html(s);
		$('.title').html('幸运观众');
		$('.region3').css('display','none');
		$('.region1').css('display','flex');
	}
	else{
		var n=localStorage.getItem('total_of_event');
		if(n==0) alert('当前没有抽奖事件！');
		else{
			var num,prize,rank;
			var n=localStorage.getItem('total_of_event');
			for(var i=0;i<n;i++){
				var id=localStorage.getItem('event_id'+String(i));
				if(id==1){
					prize=localStorage.getItem('event_rank'+String(i));
					num=localStorage.getItem('event_amount'+String(i));
					break;
				}
			}
			n=localStorage.getItem('total_of_prize');
			var flag=0;
			for(var i=0;i<n;i++){
				rank=localStorage.getItem('rank'+String(i));
				if(rank==prize){
					prize=localStorage.getItem('prize'+String(i));
					flag=1;
					break;
				}
			}
			if(!flag){
				alert('未检测到奖品信息！');
				return;
			}
			console.log(num);
			n=localStorage.getItem('total_of_people');
			var n0=localStorage.getItem('expected');
			var rest=localStorage.getItem('rest');
			console.log(num);
			console.log(n0);
			console.log(rest);
			console.log(n);
			console.log(n<1*n0);
			if(n0!=null&&n<1.0*n0) num=Math.ceil(1.0*num/n0*n);
			num=Math.min(num,rest);
			switch(rank){
				case '1':{
					rank="一等奖";
					break;
				}
				case '2':{
					rank="二等奖";
					break;
				}
				case '3':{
					rank="三等奖";
					break;
				}
				default:{
					rank="参与奖";
					break;
				}
			}
			n=localStorage.getItem('total_of_prize');
			for(var i=0;i<n;i++){
				var prize_name=localStorage.getItem('prize'+String(i));
				if(prize_name==prize){
					var new_amount=localStorage.getItem('amount'+String(i))-num;
					localStorage.setItem('amount'+String(i),new_amount);
				}
			}
			localStorage.setItem('rest',rest-num);
			n=localStorage.getItem('total_of_event');
			for(var i=0;i<n;i++){
				var id=localStorage.getItem('event_id'+String(i));
				if(id==1){
					for(var j=i;j<n-1;j++){
						localStorage.setItem('event_id'+String(j),localStorage.getItem('event_id'+String(j+1)));
						localStorage.setItem('event_rank'+String(j),localStorage.getItem('event_rank'+String(j+1)));
						localStorage.setItem('event_amount'+String(j),localStorage.getItem('event_amount'+String(j+1)));
					}
					n--;
					localStorage.removeItem('event_id'+String(n));
					localStorage.removeItem('event_rank'+String(n));
					localStorage.removeItem('event_amount'+String(n));
					localStorage.setItem('total_of_event',n);
					for(var j=0;j<n;j++) localStorage.setItem('event_id'+String(j),localStorage.getItem('event_id'+String(j))-1);
					break;
				}
			}
			var cnt=1;
			for(var i=1;i<=num;i++,cnt++){
				var id=get_random();
				s=s+person[id];
				if(cnt==4) s+='<br>',cnt=0;
				else s+='&emsp;';
				vis[id]=1;
				localStorage.setItem('vis'+String(id),1);
				localStorage.setItem('state'+String(id),rank);
			}
			$('#first').html(s);
			$('.title').html(rank);
			$('.tip').html('当前共有'+String(localStorage.getItem('rest'))+'人未中奖');
			$('.region3').css('display','none');
			$('.region1').css('display','flex');
		}
	}
}
function reset(){
	$('#first').html('');
	$('.region3').css('display','flex')
	$('.region1').css('display','none');
	for(var i=0;i<n;i++){
		vis[i]=0;
		localStorage.removeItem('vis'+String(i));
		localStorage.removeItem('state'+String(i));
	}
	localStorage.setItem('rest',localStorage.getItem('total_of_people'));
	$('.tip').html('当前共有'+String(localStorage.getItem('rest'))+'人未中奖');
}

function help(){
	$('.help').css('display','block');
}
function help_sure(){
	$('.help').css('display','none');
}